package com.can.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.can.reggie.common.R;
import com.can.reggie.entity.Orders;
import com.can.reggie.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;


    @GetMapping("/page")
    public R<Page<Orders>> page(int page, int pageSize, String number, String beginTime,String endTime) {
        System.out.println(page+"--"+pageSize+"--"+number+"--"+beginTime+"--"+endTime);

        //创建分页构造器
        Page<Orders> pageInfo = new Page<>(page,pageSize);

        //创建条件构造器
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.isNotEmpty(number),Orders::getNumber,number);
        lqw.between(StringUtils.isNotEmpty(beginTime)&&StringUtils.isNotEmpty(endTime),Orders::getOrderTime,beginTime,endTime);

        Page<Orders> ordersPage = orderService.page(pageInfo, lqw);


        return R.success(ordersPage);
    }


    /**
     * 用户下单
     *
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {

        orderService.submit(orders);
        return R.success("下单成功");
    }
}